# -*- coding: utf-8 -*-

from odoo import models, fields, api


class CpqChangeItem(models.Model):
    _name = 'cpq.change.item'
    _description = '变更条目'
    _order = 'sequence, id'
    
    # ============ 基础信息 ============
    change_request_id = fields.Many2one(
        'cpq.change.request',
        string='变更请求',
        required=True,
        ondelete='cascade'
    )
    
    sequence = fields.Integer(
        string='序号',
        default=10
    )
    
    # ============ 变更对象 ============
    change_object = fields.Selection([
        ('material', '材料变更'),
        ('dimension', '尺寸变更'),
        ('process', '工艺变更'),
        ('component', '组件变更'),
        ('configuration', '配置变更')
    ], string='变更对象', required=True)
    
    # ============ 变更前后 ============
    field_name = fields.Char(
        string='变更字段'
    )
    
    old_value = fields.Char(
        string='原值'
    )
    
    new_value = fields.Char(
        string='新值'
    )
    
    # ============ 成本影响 ============
    cost_delta = fields.Monetary(
        string='成本变化',
        currency_field='currency_id',
        help='正值表示成本增加，负值表示成本减少'
    )
    
    currency_id = fields.Many2one(
        'res.currency',
        related='change_request_id.currency_id',
        string='货币'
    )
    
    # ============ 备注 ============
    description = fields.Text(
        string='描述'
    )
    
    notes = fields.Text(
        string='备注'
    )
    
    # ============ 业务方法 ============
    def _apply_to_configuration(self, configuration):
        """将变更应用到配置"""
        # TODO: 根据change_object类型，更新配置
        if self.change_object == 'material':
            # 更新材料
            pass
        elif self.change_object == 'component':
            # 更新组件
            pass
        elif self.change_object == 'configuration':
            # 更新配置项
            pass

